package com.wanshi.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import javax.servlet.http.HttpSessionBindingEvent;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

@WebListener()
public class MyServletContextListener implements ServletContextListener{

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        Properties prop = new Properties();

        String path = this.getClass().getClassLoader().getResource("cust.properties").getPath();

        try {
            FileReader fr =new FileReader(path);
            prop.load(fr);

            String picUrl = prop.getProperty("pic_url");
            String picSavePath = prop.getProperty("pic_save_path");

            //把值存入servletContext
            ServletContext context = sce.getServletContext();
            context.setAttribute("pic_url",picUrl);
            context.setAttribute("pic_save_path",picSavePath);
            System.out.println("监听到servletContext启动----存入图片路径"+picSavePath);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}
